/**
 * @author xieyue
 * @date 创建时间：2015年1月29日 上午11:13:52
 * @version 1.0
 */
package com.ciwong.epaper.modules.me.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.ui.MeFragment;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.eyeguard.EyeGuardModeView;

import java.util.List;


public class ListViewAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> datas;
    private LayoutInflater mInflater;
    private int[] iconArray = {R.mipmap.icn_learn_setting, R.mipmap.icon_eyeshield, R.mipmap.icon_clear_cache, R.mipmap.icon_mine_scan, R.mipmap.icon_que_ans,R.mipmap.icn_tousu, R.mipmap.icon_phone3};

    public ListViewAdapter(Context mContext, List<String> datas) {
        this.mContext = mContext;
        this.datas = datas;
        mInflater = LayoutInflater.from(mContext);
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_listview, null);
            holder = new ViewHolder();
            holder.itemView = (TextView) convertView.findViewById(R.id.item_entrance_name);
            holder.tx_service_number = (TextView) convertView.findViewById(R.id.tx_service_number);
            holder.img_arrow_right = (ImageView) convertView.findViewById(R.id.img_arrow_right);
            holder.img_entrance_type = (ImageView) convertView.findViewById(R.id.img_entrance_type);
            holder.img_entrance_tips = (ImageView) convertView.findViewById(R.id.img_entrance_tips);
            holder.iv_eye_guard = (ImageView) convertView.findViewById(R.id.iv_eye_guard);
            holder.mRyeGuardRl = (RelativeLayout) convertView.findViewById(R.id.rl_eye_guard);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.itemView.setText(datas.get(position));

        if (position < iconArray.length) {
            holder.img_entrance_type.setImageResource(iconArray[position]);
        }

        if (position == 1) {
            holder.tx_service_number.setVisibility(View.GONE);
            holder.iv_eye_guard.setVisibility(View.VISIBLE);
            holder.mRyeGuardRl.setVisibility(View.VISIBLE);
            holder.img_entrance_tips.setVisibility(View.VISIBLE);
            holder.img_arrow_right.setVisibility(View.GONE);
            //护眼模式设置
            final UserInfoBase userInfoBase = EApplication.getInstance().getUserInfoBase();
            if (userInfoBase != null) {
                boolean isEyeGuardMode = ESystem.getSharedBoolean(MeFragment.IS_USE_EYE_GUARD_MODE + userInfoBase.getUserId(), false);
                holder.iv_eye_guard.setSelected(isEyeGuardMode);
                if (isEyeGuardMode) {
                    EyeGuardModeView.showEyeGuard(true);
                }
                EApplication.getInstance().isEyeGuardModeOpen = isEyeGuardMode;
            }
            holder.mRyeGuardRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnEyeGuardOpenListener != null) {
                        mOnEyeGuardOpenListener.isEyeGuardOpen(holder.iv_eye_guard, !holder.iv_eye_guard.isSelected());
                    }
                }
            });
        } else {
            holder.tx_service_number.setVisibility(View.GONE);
            holder.iv_eye_guard.setVisibility(View.GONE);
            holder.img_entrance_tips.setVisibility(View.GONE);
            holder.mRyeGuardRl.setVisibility(View.GONE);
            holder.img_arrow_right.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public OnEyeGuardOpenListener getOnEyeGuardOpenListener() {
        return mOnEyeGuardOpenListener;
    }

    public void setOnEyeGuardOpenListener(OnEyeGuardOpenListener onEyeGuardOpenListener) {
        mOnEyeGuardOpenListener = onEyeGuardOpenListener;
    }

    OnEyeGuardOpenListener mOnEyeGuardOpenListener;

    public interface OnEyeGuardOpenListener {
        void isEyeGuardOpen(ImageView imageView, boolean isEyeGuardOpen);
    }

    class ViewHolder {
        TextView itemView;
        ImageView img_entrance_type;
        ImageView img_entrance_tips;
        TextView tx_service_number;
        ImageView img_arrow_right;
        RelativeLayout mRyeGuardRl;
        ImageView iv_eye_guard;
    }


}
